import { Activity, Actor, AnswersQuestions, PerformsActivities, Task, UsesAbilities } from '@serenity-js/core';
import { by, Click, Enter, Navigate, Target } from '@serenity-js/webdriverio';
import { config } from '../../config';
import { UseSystem } from '../abilities';

const { the } = Target;

const loginPage = {
  usernameInput: the('username input').located(by.id('username')),
  passwordInput: the('password input').located(by.id('password')),
  loginButton: the('login button').located(by.id('Login')),
};

export class Login implements Activity {
  public static viaLoginPage(): Activity {
    return new Login();
  }

  performAs(actor: Actor): any {
    const useSalesforceAbility = actor.abilityTo(UseSystem);
    const username = useSalesforceAbility.username();
    const password = useSalesforceAbility.password();
    return actor.attemptsTo(
      Navigate.to(config.loginUrl),
      Enter.theValue(username).into(loginPage.usernameInput),
      Enter.theValue(password).into(loginPage.passwordInput),
      Click.on(loginPage.loginButton)
    );
  }

  toString(): string {
    return '#actor tries to log in via login page';
  }
}
